package com.tanhua.server.controller;

import com.tanhua.model.enums.UserState;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.UserFreezeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: Liking
 * @description
 * @date: 2023/7/28 16:05
 */
@RestController
@RequestMapping("/comments")
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentsService;

    @Autowired
    private UserFreezeService userFreezeService;

    //分页查询评理列表
    @GetMapping
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String movementId) {
        PageResult pr = commentsService.findComments(movementId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 发布评论
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map) {
        String movementId = (String )map.get("movementId");
        String comment = (String)map.get("comment");
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //判断当前用户是否被冻结
        userFreezeService.checkUserStatus(UserState.COMMENT.getType(),userId);
        commentsService.saveComments(movementId,comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 点赞评论
     */
    @GetMapping("/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String commentId) {
        Integer likeCount = commentsService.like(commentId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞评论
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislikeComment(@PathVariable("id") String commentId) {
        Integer likeCount = commentsService.dislike(commentId);
        return ResponseEntity.ok(likeCount);
    }


}
